import { observable, action } from 'mobx';
import { Form } from 'weaver-mobile-page';
import * as adapter from '../../constants/adapter';
import { Tools, Modal, Bubble } from 'weaver-mobile';

const alert = Modal.alert;
const getLabel = Tools.getLabel;

export default class AuthorityAddStore {
    @observable form = {};
    @observable buttonProps = {};
    @observable complete = false;
    @observable props;

    @action
    initForm(props) {
        const currentPath = props.location.pathname;
        this.form = new Form.Store({route:currentPath});
        this.props = props;
        this.buttonProps = { loading: false, disabled: false };
        Tools.callApi({
            url: '/api/crm/mobile/getAuthorityForm',
        }).then((res) => {
            this.complete = true;
            this.form.initForm(res.datas, currentPath);
        });
    }

    @action
    buttonClick() {
        let visitor = this.props.location.search;
        visitor = visitor.split('?')[1];
        if(!visitor){
            visitor = this.props.params.id;
        }
        if (!visitor) {
            Bubble.error(getLabel('390950','缺少必要的参数'), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2);
            return;
        }
        if (this.form.doValidate().pass) {
            this.buttonProps.disabled = true;
            this.buttonProps.loading = true;
            Tools.callApi({
                url: '/api/crm/customer/addShare',
                params: { ...this.form.formParams, customerId: visitor },
            }).then((res) => {
                this.buttonProps.disabled = false;
                this.buttonProps.loading = false;
                if (res.status === "failed") {
                    alert('',res.msgcode, [
                        {
                            text: 'ok',
                            onPress: () => { }
                        },
                    ])
                } else {
                    Bubble.success(getLabel('83551',"保存成功"), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2,()=>this.props.history.go(-1));
                }
            });
        }
    }
}